package com.xuequ.answerme.details;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.AnswerBean;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.AnswerManager;
import com.xuequ.answerme.manager.QuestionManager;
import com.xuequ.answerme.manager.UserManager;

import java.util.List;

import cn.bmob.v3.BmobUser;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by Shijie on 2015/10/12.
 */
public class AnswerDetailActivity extends AppCompatActivity {

    private Toolbar toolbar;

    private TextView mAnswerContent;
    private TextView mAnswerer;
    private TextView mLikeCount;
    private TextView mQuestionTitle;
    private ScrollView mScrollView;
    private ImageView mLike;
    private ImageView mDetail;
    private ImageView mEdit;

    private MaterialDialog materialDialogFailure;
    private MaterialDialog materialDialogQuestion;

    private AnswerBean answerBean;
    private UserBean userBean;
    private QuestionBean questionBean;

    private AnswerManager answerManager;
    private QuestionManager questionManager;
    private UserManager userManager;

    private boolean isLike = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_detail);

        initViews();//初始化控件和数据
    }

    private void initViews() {

        if (answerManager == null) {
            answerManager = new AnswerManager();
        }
        if (questionManager == null) {
            questionManager = new QuestionManager();
        }
        if (userManager == null) {
            userManager = new UserManager();
        }

        toolbar = (Toolbar) findViewById(R.id.answer_detail_tool_bar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        answerBean = (AnswerBean) bundle.getSerializable("answerBean");
        Log.i("AnswerDetailActivity","answerBean content````````````````````"+answerBean.getAnswer_content());

        userBean = BmobUser.getCurrentUser(this, UserBean.class);

        mScrollView = (ScrollView) findViewById(R.id.answer_detail_scroll);
        mQuestionTitle = (TextView) findViewById(R.id.answer_detail_question_title_text);
        mAnswerer = (TextView) findViewById(R.id.answer_detail_content_answerer_text);
        mLikeCount = (TextView) findViewById(R.id.answer_detail_content_like_text);
        mAnswerContent = (TextView) findViewById(R.id.answer_detail_content_text);
        mDetail = (ImageView) findViewById(R.id.answer_detail_question_img);
        mEdit = (ImageView) findViewById(R.id.answer_detail_edit_img);
        mLike = (ImageView) findViewById(R.id.answer_detail_like_img);

        checkIsLike();
        answerManager.checkTheAnswer(AnswerDetailActivity.this, answerBean.getObjectId(), new AnswerManager.CheckTheAnswerListener() {
            @Override
            public void checkTheAnswerOnSuccess(AnswerBean answer) {

                answerBean = answer;
                questionBean = answer.getAnswer_of_question();

                mEdit.setClickable(true);
                mDetail.setClickable(true);

                if (userBean.getUsername().equals(answerBean.getAnswerer().getUsername())) {
                    mEdit.setVisibility(View.VISIBLE);
                }

                mQuestionTitle.setText(answer.getAnswer_of_question().getQuestion_title());
                mAnswerer.setText(answer.getAnswerer().getUsername());
                mLikeCount.setText(answer.getAnswer_likes().toString());
                mAnswerContent.setText(answer.getAnswer_content());

                mScrollView.setVisibility(View.VISIBLE);
            }

            @Override
            public void checkTheAnswerOnFailure(String failureMsg) {
                questionDetail();
            }
        });


        mLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLike.setClickable(false);
                userManager.likeOrNot(AnswerDetailActivity.this, answerBean, userBean, isLike, new UserManager.LikeOrNotListener() {
                    @Override
                    public void likeOrNotOnSuccess() {
                        answerManager.likeNumberChange(AnswerDetailActivity.this, answerBean.getObjectId(), isLike, new AnswerManager.LikeNumberChangeListener() {
                            @Override
                            public void likeNumberChangeOnSuccess() {
                                isLike = !isLike;
                                if (isLike) {
                                    mLike.setImageResource(R.drawable.am_isfocus);
                                } else {
                                    mLike.setImageResource(R.drawable.am_focus);
                                }
                                mLike.setClickable(true);
                            }

                            @Override
                            public void likeNumberChangeOnFailure(String failureMsg) {
                                mLike.setClickable(true);
                            }
                        });
                    }

                    @Override
                    public void likeOrNotOnFailure(String failureMsg) {
                        mLike.setClickable(true);
                    }
                });
            }
        });

        //mEdit onClick


        mDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                questionDetail();
            }
        });

    }

    private void checkIsLike() {
        userManager.checkIsLike(this, userBean, new UserManager.CheckIsLikeOrFocusListener() {
            @Override
            public void checkIsLikeOrFocusOnSuccess(List<AnswerBean> answerBeans) {
                if (answerBeans.size() != 0) {
                    mLike.setImageResource(R.drawable.am_isfocus);
                    isLike = true;
                }
                mLike.setClickable(true);
            }

            @Override
            public void checkIsLikeOrFocusOnFailure(String failureMsg) {
                mLike.setClickable(true);
            }
        });

    }

    private void showQuestionDismiss() {
        materialDialogFailure = new MaterialDialog(this);
        materialDialogFailure.setTitle(":(");
        materialDialogFailure.setMessage("此问题可能已经被删除或者网络原因无法查看，请点击OK返回上一层");
        materialDialogFailure.setPositiveButton("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDialogFailure.dismiss();
                finish();
            }
        });
        materialDialogFailure.show();
    }

    private void questionDetail() {
        materialDialogQuestion = new MaterialDialog(this);
        materialDialogQuestion.setTitle(questionBean.getQuestion_title());
        materialDialogQuestion.setMessage(questionBean.getQuestion_description());
        materialDialogQuestion.setPositiveButton("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDialogQuestion.dismiss();
            }
        });
        materialDialogQuestion.show();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
